﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlSucursal
    {
        private ConexionDataContext conexion;

        internal List<CONEXION.SUCURSAL> getListSucursal()
        {
            using (conexion = new ConexionDataContext())
            {
                var query = conexion.SUCURSAL.Where(x => x.eliminado == 0).ToList();
                return query;
            }
        } 


        internal System.Windows.Forms.DialogResult registrarSucursal(SUCURSAL suc)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SUCURSAL sucursal = suc;
                        DialogResult result = DialogResult.Cancel;

                      
                        //insertamos el id
                        int correlativoSucursal = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").SUCURSAL.Value;
                        string num = Convert.ToString(correlativoSucursal);
                        string numero = num.Trim().PadLeft(10, '0');
                        string idSucursal = "SUCU_" + numero;

                        suc.idSucursal = idSucursal;

                        //insertamos el sector
                        conexion.SUCURSAL.InsertOnSubmit(suc);
                        conexion.SubmitChanges();
                        conexion.increm_ContSucursal();


                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal System.Windows.Forms.DialogResult actualizarSucursal(SUCURSAL suc)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SUCURSAL sucur = conexion.SUCURSAL.Single(x => x.idSucursal == suc.idSucursal);
                        DialogResult result = DialogResult.Cancel;
                        //actualizamos
                        sucur.nombreSucursal = suc.nombreSucursal;
                        sucur.eliminado = suc.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarSucursal(SUCURSAL suc)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SUCURSAL sucur = conexion.SUCURSAL.Single(x => x.idSucursal == suc.idSucursal);
                        //actualizamos
                        sucur.eliminado = suc.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
    }
}
